package inits

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"lesson/config"
	"lesson/global"
	"log"
)

func InitMysql() {
	var err error
	sql := config.AppConf.MysqlConfig
	// https://github.com/go-sql-driver/mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=True&loc=Local", sql.User, sql.Pass, sql.Host, sql.Port, sql.MysqlDB)

	global.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
		Logger: logger.Default.LogMode(logger.Info),
	})
	if err != nil {
		panic("mysql link fail")
	}
	log.Println("mysql link success")
}
